package cn.lizemin.demo.WeakReference;

import sun.nio.cs.SingleByte;

import java.lang.ref.WeakReference;

/**
 * @author: lizemin
 * @version: 1.0
 */
public class Demo {
    //这是一个main方法,是程序的入口
    public static void main(String[] args) throws InterruptedException {
        Car car = new Car("BYD");
        //使用weakReference包装car对象
        WeakReference<Car> weakCar = new WeakReference<>(car);
        //主动GC
        System.gc();
        //可以通过weakCar.get()获取引用
        System.out.println("强引用存在时：======>"+weakCar.get());

        //切断原始的强引用
        car = null;
        Thread.sleep(3 * 1000L);
        System.out.println("只剩弱引用，但是还没有GC时：==========>" + weakCar.get());

        //主动GC
        System.gc();
        System.out.println("只剩弱引用，且GC后：========>" + weakCar.get());


    }
    static class Car{
        private String name ; //

        public Car(String name) {
            this.name = name;
        }
    }

}
